package com;


import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;



//@EnableSwagger2
@SpringBootApplication(exclude = {DataSourceTransactionManagerAutoConfiguration.class})//(scanBasePackages = "com.suncnpap")
@ServletComponentScan
@EnableScheduling
@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class,MultipartAutoConfiguration.class}) //防止阿里连接池出错
@MapperScan("com.*.**.mapper")
public class AppStart extends SpringBootServletInitializer {
	@Value("${sysConfig.maxUploadSize}")
	private int maxUploadSize;
	
	
	private static final Logger log = Logger.getLogger(AppStart.class);
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    	
    	SpringApplicationBuilder b = builder.sources(AppStart.class);
    	
        return b;
    }
   
	
	public static void main(String[] args) {
		
        SpringApplication.run(AppStart.class,args);
 
        
	}
	
	
	
	
	
	//显示声明CommonsMultipartResolver为mutipartResolver
    @Bean(name = "multipartResolver")
	public MultipartResolver multipartResolver(){
	    CommonsMultipartResolver resolver = new CommonsMultipartResolver() {
			@Override
			public boolean isMultipart(HttpServletRequest request) {
				// TODO Auto-generated method stub
				String url = request.getRequestURI();
				if (url != null && url.contains("ued/config"))		//过滤掉百度上传文件路劲，保证百度上传不被SpringMVC 的文件上传拦截
				{
					return false;
				}else{
					return super.isMultipart(request);
				}
			}
	    };
	    
	    resolver.setDefaultEncoding("UTF-8");
	    resolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
	    resolver.setMaxInMemorySize(10*1024*1024);			//存放在内存中的大小，大于此将生成临时文件
	    resolver.setMaxUploadSize(maxUploadSize*1024*1024);//上传文件大小 50M 50*1024*1024
	    return resolver;
    }
}
